//
//  PeriodsSettingVC.m
//  FishTank
//
//  Created by Scott Zhu on 14/12/11.
//  Copyright (c) 2014年 Scott Zhu. All rights reserved.
//

#import "PeriodsSettingVC.h"

@interface PeriodsSettingVC ()
@property (strong, nonatomic) NSArray *pickerViewData;
@property (strong, nonatomic) UITapGestureRecognizer *tapGestureRecognizer;
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) UIAlertView *timePickerAlertView;
@end

@implementation PeriodsSettingVC
@synthesize pickerViewData = _pickerViewData;
@synthesize controlPeriods = _controlPeriods;
@synthesize deviceInfo = _deviceInfo;
@synthesize pickerView = _pickerView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
    [self.period1OpenTime addTarget:self action:@selector(timeLabelTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.period1CloseTime addTarget:self action:@selector(timeLabelTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.period2OpenTime addTarget:self action:@selector(timeLabelTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.period2CloseTime addTarget:self action:@selector(timeLabelTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.period3OpenTime addTarget:self action:@selector(timeLabelTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.period3CloseTime addTarget:self action:@selector(timeLabelTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.factorySettingButton.hidden = YES;
    [self _initButtonView];
    
}

- (void)_initButtonView{
    
    
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT - 150, SCREEN_WIDTH, 30)];
    
    [self.view addSubview:footer];
    
    
    // 创建确定按钮
    UIButton  * determineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [footer addSubview:determineBtn];
    [determineBtn setTitle:@"确定" forState:UIControlStateNormal];
    [determineBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [determineBtn setBackgroundColor:SS_DEVICE_BLUE_COLOR];
    determineBtn.layer.borderColor = SS_DEVICE_BLUE_COLOR.CGColor;
    determineBtn.layer.borderWidth = 1;
    determineBtn.layer.masksToBounds = YES;
    determineBtn.layer.cornerRadius = 3.0;
    
    
    // 创建取消按钮
    UIButton  * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [footer addSubview:cancelBtn];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setBackgroundColor:[UIColor lightGrayColor]];
    cancelBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 3.0;
    
    
    

    
    // 创建恢复出厂按钮
    UIButton  * restoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [footer addSubview:restoreBtn];
    [restoreBtn setTitle:@"恢复出厂" forState:UIControlStateNormal];
    [restoreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [restoreBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [restoreBtn setBackgroundColor:[UIColor lightGrayColor]];
    restoreBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    restoreBtn.layer.borderWidth = 1;
    restoreBtn.layer.masksToBounds = YES;
    restoreBtn.layer.cornerRadius = 3.0;

    
    
    
    
    [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(footer.mas_centerY);
        make.left.equalTo(footer).offset(30);
        make.right.equalTo(cancelBtn.mas_left).offset(-10);
        make.height.equalTo(@30);
        make.height.equalTo(cancelBtn);
        make.height.equalTo(restoreBtn);
        make.width.equalTo(cancelBtn);
        make.width.equalTo(restoreBtn);
    }];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(footer.mas_centerY);
        make.left.equalTo(determineBtn.mas_right).offset(10);
        make.right.equalTo(restoreBtn.mas_left).offset(-10);

    
    }];

    [restoreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(footer.mas_centerY);
        make.right.equalTo(footer).offset(-30);
    }];
}

- (void)buttonAction:(UIButton *)button{
    NSString *title = [button titleForState:UIControlStateNormal];
    if ([title isEqualToString:@"确定"]) {
        NSLog(@"确定");
        
        if(![[APIs sharedInstance] checkPPPP]){
            //会话失效
            [[APIs sharedInstance] reconnect:^(NSInteger session){
                [[APIs sharedInstance] setControlPeriod:_controlPeriods
                                               password:_deviceInfo.password
                                                success:^{
                                                    [[[UIAlertView alloc] initWithTitle:@"✅设置成功"
                                                                                message:nil
                                                                               delegate:self
                                                                      cancelButtonTitle:@"好的"
                                                                      otherButtonTitles:nil, nil] show];
                                                }
                                                failure:^(NSInteger errorCode) {
                                                    [[[UIAlertView alloc] initWithTitle:@"❌设置失败"
                                                                                message:@"网络异常，请稍候重试"
                                                                               delegate:nil
                                                                      cancelButtonTitle:@"好的"
                                                                      otherButtonTitles:nil, nil] show];
                                                }];

            } failure:^(NSInteger errCode){
                [SVProgressHUD showErrorWithStatus:@"设备断开连接,请重试" withDuration:3];
            }];
            
        }else{
            [[APIs sharedInstance] setControlPeriod:_controlPeriods
                                           password:_deviceInfo.password
                                            success:^{
                                                [[[UIAlertView alloc] initWithTitle:@"✅设置成功"
                                                                            message:nil
                                                                           delegate:self
                                                                  cancelButtonTitle:@"好的"
                                                                  otherButtonTitles:nil, nil] show];
                                            }
                                            failure:^(NSInteger errorCode) {
                                                [[[UIAlertView alloc] initWithTitle:@"❌设置失败"
                                                                            message:@"网络异常，请稍候重试"
                                                                           delegate:nil
                                                                  cancelButtonTitle:@"好的"
                                                                  otherButtonTitles:nil, nil] show];
                                            }];

        }

       
    
    }
    if ([title isEqualToString:@"取消"]) {
        NSLog(@"取消");
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if ([title isEqualToString:@"恢复出厂"]) {
        NSLog(@"恢复出厂");
        if (_controlPeriods.type  ==  Light ) {
            _controlPeriods.period1.startHour = 7;
            _controlPeriods.period1.startMinute = 0;
            _controlPeriods.period1.endHour = 11;
            _controlPeriods.period1.endMinutes = 0;
            
            _controlPeriods.period2.startHour = 12;
            _controlPeriods.period2.startMinute = 0;
            _controlPeriods.period2.endHour = 17;
            _controlPeriods.period2.endMinutes = 0;
            
            
            _controlPeriods.period3.startHour = 18;
            _controlPeriods.period3.startMinute = 0;
            _controlPeriods.period3.endHour = 21;
            _controlPeriods.period3.endMinutes = 0;
            
        }else{
            
            _controlPeriods.period1.startHour = 6;
            _controlPeriods.period1.startMinute = 0;
            _controlPeriods.period1.endHour = 6;
            _controlPeriods.period1.endMinutes = 11;
            
            _controlPeriods.period2.startHour = 11;
            _controlPeriods.period2.startMinute = 0;
            _controlPeriods.period2.endHour = 11;
            _controlPeriods.period2.endMinutes = 11;
            
            
            _controlPeriods.period3.startHour = 17;
            _controlPeriods.period3.startMinute = 0;
            _controlPeriods.period3.endHour = 17;
            _controlPeriods.period3.endMinutes = 11;
        }

        
        
        if(![[APIs sharedInstance] checkPPPP]){
            //会话失效
            [[APIs sharedInstance] reconnect:^(NSInteger session){
                [[APIs sharedInstance] setControlPeriod:_controlPeriods
                                               password:_deviceInfo.password
                                                success:^{
                                                    [[[UIAlertView alloc] initWithTitle:@"✅设置成功"
                                                                                message:nil
                                                                               delegate:self
                                                                      cancelButtonTitle:@"好的"
                                                                      otherButtonTitles:nil, nil] show];
                                                }
                                                failure:^(NSInteger errorCode) {
                                                    [[[UIAlertView alloc] initWithTitle:@"❌设置失败"
                                                                                message:@"网络异常，请稍候重试"
                                                                               delegate:nil
                                                                      cancelButtonTitle:@"好的"
                                                                      otherButtonTitles:nil, nil] show];
                                                }];
                

            } failure:^(NSInteger errCode){
                [SVProgressHUD showErrorWithStatus:@"设备断开连接,请重试" withDuration:3];
            }];
            
        }else{
            
            
            if(![[APIs sharedInstance] checkPPPP]){
                //会话失效
                [[APIs sharedInstance] reconnect:^(NSInteger session){
                    [[APIs sharedInstance] setControlPeriod:_controlPeriods
                                                   password:_deviceInfo.password
                                                    success:^{
                                                        [[[UIAlertView alloc] initWithTitle:@"✅设置成功"
                                                                                    message:nil
                                                                                   delegate:self
                                                                          cancelButtonTitle:@"好的"
                                                                          otherButtonTitles:nil, nil] show];
                                                    }
                                                    failure:^(NSInteger errorCode) {
                                                        [[[UIAlertView alloc] initWithTitle:@"❌设置失败"
                                                                                    message:@"网络异常，请稍候重试"
                                                                                   delegate:nil
                                                                          cancelButtonTitle:@"好的"
                                                                          otherButtonTitles:nil, nil] show];
                                                    }];

                } failure:^(NSInteger errCode){
                    [SVProgressHUD showErrorWithStatus:@"设备断开连接,请重试" withDuration:3];
                }];
                
            }else{
                
                
                [[APIs sharedInstance] setControlPeriod:_controlPeriods
                                               password:_deviceInfo.password
                                                success:^{
                                                    [[[UIAlertView alloc] initWithTitle:@"✅设置成功"
                                                                                message:nil
                                                                               delegate:self
                                                                      cancelButtonTitle:@"好的"
                                                                      otherButtonTitles:nil, nil] show];
                                                }
                                                failure:^(NSInteger errorCode) {
                                                    [[[UIAlertView alloc] initWithTitle:@"❌设置失败"
                                                                                message:@"网络异常，请稍候重试"
                                                                               delegate:nil
                                                                      cancelButtonTitle:@"好的"
                                                                      otherButtonTitles:nil, nil] show];
                                                }];

            }

            

        }

       
    }
    
}




- (void)viewDidAppear:(BOOL)animated
{
}

#pragma -mark- Custom Methods
- (void)initUI
{

    self.factorySettingButton.layer.borderWidth = 1;
    self.factorySettingButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.factorySettingButton.layer.cornerRadius = 3;
    
    [self.period1OpenTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period1.startHour, _controlPeriods.period1.startMinute] forState:UIControlStateNormal];
    [self.period1CloseTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period1.endHour, _controlPeriods.period1.endMinutes] forState:UIControlStateNormal];
    
    [self.period2OpenTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period2.startHour, _controlPeriods.period2.startMinute] forState:UIControlStateNormal];
    [self.period2CloseTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period2.endHour, _controlPeriods.period2.endMinutes] forState:UIControlStateNormal];
    
    [self.period3OpenTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period3.startHour, _controlPeriods.period3.startMinute] forState:UIControlStateNormal];
    [self.period3CloseTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period3.endHour, _controlPeriods.period3.endMinutes] forState:UIControlStateNormal];
    
    
    [self.period1OpenTime setTitleColor:SS_DEVICE_BLUE_COLOR forState:UIControlStateNormal];
    [self.period1CloseTime setTitleColor:SS_DEVICE_BLUE_COLOR forState:UIControlStateNormal];
    [self.period2OpenTime setTitleColor:SS_DEVICE_BLUE_COLOR forState:UIControlStateNormal];
    [self.period2CloseTime setTitleColor:SS_DEVICE_BLUE_COLOR forState:UIControlStateNormal];
    [self.period3OpenTime setTitleColor:SS_DEVICE_BLUE_COLOR forState:UIControlStateNormal];
    [self.period3CloseTime setTitleColor:SS_DEVICE_BLUE_COLOR forState:UIControlStateNormal];
    

    
    self.period1OpenLabel.textColor = SS_DEVICE_BLUE_COLOR;
    self.period2OpenLabel.textColor = SS_DEVICE_BLUE_COLOR;
    self.period3OpenLabel.textColor = SS_DEVICE_BLUE_COLOR;
    
    
    switch (_controlPeriods.type)
    {
        case Light:
            break;
        case Water:
            [self.period1OpenLabel setText:@"冲浪泵开启时间"];
            [self.period1CloseLabel setText:@"冲浪泵关闭时间"];
            [self.period2OpenLabel setText:@"冲浪泵开启时间"];
            [self.period2CloseLabel setText:@"冲浪泵关闭时间"];
            [self.period3OpenLabel setText:@"冲浪泵开启时间"];
            [self.period3CloseLabel setText:@"冲浪泵关闭时间"];
            
            [self.period1OpenImageview setImage:[UIImage imageNamed:@"TimeWaterOn"]];
            [self.period1CloseImageview setImage:[UIImage imageNamed:@"TimeWaterOff"]];
            [self.period2OpenImageview setImage:[UIImage imageNamed:@"TimeWaterOn"]];
            [self.period2CloseImageview setImage:[UIImage imageNamed:@"TimeWaterOff"]];
            [self.period3OpenImageview setImage:[UIImage imageNamed:@"TimeWaterOn"]];
            [self.period3CloseImageview setImage:[UIImage imageNamed:@"TimeWaterOff"]];
            break;
        case Disinfect:
            [self.period1OpenLabel setText:@"杀菌灯开启时间"];
            [self.period1CloseLabel setText:@"杀菌灯关闭时间"];
            [self.period2OpenLabel setText:@"杀菌灯开启时间"];
            [self.period2CloseLabel setText:@"杀菌灯关闭时间"];
            [self.period3OpenLabel setText:@"杀菌灯开启时间"];
            [self.period3CloseLabel setText:@"杀菌灯关闭时间"];
            
            [self.period1OpenImageview setImage:[UIImage imageNamed:@"TimeDisinfectOn"]];
            [self.period1CloseImageview setImage:[UIImage imageNamed:@"TimeDisinfectOff"]];
            [self.period2OpenImageview setImage:[UIImage imageNamed:@"TimeDisinfectOn"]];
            [self.period2CloseImageview setImage:[UIImage imageNamed:@"TimeDisinfectOff"]];
            [self.period3OpenImageview setImage:[UIImage imageNamed:@"TimeDisinfectOn"]];
            [self.period3CloseImageview setImage:[UIImage imageNamed:@"TimeDisinfectOff"]];
            break;
            
        default:
            break;
    }
}

-(void)timeLabelTapped:(UIButton *)sender
{
    NSLog(@"Label tag = %ld, tapped", (long)sender.tag);
    
    switch (sender.tag)
    {
        case 1:
            [self.pickerView selectRow:_controlPeriods.period1.startHour inComponent:0 animated:YES];
            [self.pickerView selectRow:_controlPeriods.period1.startMinute inComponent:1 animated:YES];
            break;
        case 2:
            [self.pickerView selectRow:_controlPeriods.period1.endHour inComponent:0 animated:YES];
            [self.pickerView selectRow:_controlPeriods.period1.endMinutes inComponent:1 animated:YES];
            break;
        case 3:
            [self.pickerView selectRow:_controlPeriods.period2.startHour inComponent:0 animated:YES];
            [self.pickerView selectRow:_controlPeriods.period2.startMinute inComponent:1 animated:YES];
            break;
        case 4:
            [self.pickerView selectRow:_controlPeriods.period2.endHour inComponent:0 animated:YES];
            [self.pickerView selectRow:_controlPeriods.period2.endMinutes inComponent:1 animated:YES];
            break;
        case 5:
            [self.pickerView selectRow:_controlPeriods.period3.startHour inComponent:0 animated:YES];
            [self.pickerView selectRow:_controlPeriods.period3.startMinute inComponent:1 animated:YES];
            break;
        case 6:
            [self.pickerView selectRow:_controlPeriods.period3.endHour inComponent:0 animated:YES];
            [self.pickerView selectRow:_controlPeriods.period3.endMinutes inComponent:1 animated:YES];
            break;
            
        default:
            break;
    }
    [self.timePickerAlertView setTitle:[NSString stringWithFormat:@"时段%ld%@时间", (sender.tag + 1) / 2, sender.tag % 2 == 0? @"关闭":@"开启"]];
    [self.timePickerAlertView setTag:sender.tag];
    [self.timePickerAlertView show];
}


- (IBAction)factorySettingPressed:(id)sender
{
    [_period1LockSwitch setOn:YES animated:YES];
    [_period2LockSwitch setOn:YES animated:YES];
    [_period3LockSwitch setOn:YES animated:YES];
    
    switch (_controlPeriods.type)
    {
        case Light:
            _controlPeriods.period1.startHour = 7;
            _controlPeriods.period1.startMinute = 0;
            _controlPeriods.period1.endHour = 11;
            _controlPeriods.period1.endMinutes = 0;
            
            _controlPeriods.period2.startHour = 12;
            _controlPeriods.period2.startMinute = 0;
            _controlPeriods.period2.endHour = 17;
            _controlPeriods.period2.endMinutes = 0;
            
            _controlPeriods.period3.startHour = 18;
            _controlPeriods.period3.startMinute = 0;
            _controlPeriods.period3.endHour = 21;
            _controlPeriods.period3.endMinutes = 0;
            break;
        case Water:
            _controlPeriods.period1.startHour = 6;
            _controlPeriods.period1.startMinute = 0;
            _controlPeriods.period1.endHour = 6;
            _controlPeriods.period1.endMinutes = 3;
            
            _controlPeriods.period2.startHour = 11;
            _controlPeriods.period2.startMinute = 0;
            _controlPeriods.period2.endHour = 11;
            _controlPeriods.period2.endMinutes = 3;
            
            _controlPeriods.period3.startHour = 17;
            _controlPeriods.period3.startMinute = 0;
            _controlPeriods.period3.endHour = 17;
            _controlPeriods.period3.endMinutes = 3;
            break;
        case Disinfect:
            _controlPeriods.period1.startHour = 6;
            _controlPeriods.period1.startMinute = 0;
            _controlPeriods.period1.endHour = 6;
            _controlPeriods.period1.endMinutes = 3;
            
            _controlPeriods.period2.startHour = 11;
            _controlPeriods.period2.startMinute = 0;
            _controlPeriods.period2.endHour = 11;
            _controlPeriods.period2.endMinutes = 3;
            
            _controlPeriods.period3.startHour = 17;
            _controlPeriods.period3.startMinute = 0;
            _controlPeriods.period3.endHour = 17;
            _controlPeriods.period3.endMinutes = 3;
            break;
            
        default:
            break;
    }
    [self initUI];
}


#pragma -mark- Setter and Getter
- (void)setControlPeriods:(ControlPeriods *)controlPeriods
{
    if (controlPeriods && _controlPeriods != controlPeriods)
    {
        _controlPeriods = controlPeriods;
        switch (_controlPeriods.type)
        {
            case Light:
                [self setTitle:@"灯光照明时段设置"];
                break;
            case Water:
                [self setTitle:@"冲浪泵时段设置"];
                break;
            case Disinfect:
                [self setTitle:@"杀菌灯时段设置"];
                break;
                
            default:
                break;
        }
    }
}

-(UIAlertView *)timePickerAlertView
{
    if (!_timePickerAlertView)
    {
        _timePickerAlertView = [[UIAlertView alloc] initWithTitle:@""
                                                          message:@""
                                                         delegate:self
                                                cancelButtonTitle:@"取消"
                                                otherButtonTitles:@"确定", nil];
        [_timePickerAlertView setValue:self.pickerView forKey:@"accessoryView"];
    }
    return _timePickerAlertView;
}

-(UIPickerView *)pickerView
{
    if (!_pickerView)
    {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 0, 50, 100)];
        [_pickerView setDataSource:self];
        [_pickerView setDelegate:self];
    }
    return _pickerView;
}

-(NSArray *)pickerViewData
{
    if (!_pickerViewData)
    {
        _pickerViewData = @[@[@"0",@"1",@"2",@"3",@"4",
                              @"5",@"6",@"7",@"8",@"9",
                              @"10",@"11",@"12",@"13",@"14",
                              @"15",@"16",@"17",@"18",@"19",
                              @"20",@"21",@"22",@"23"]
                            ,@[@"0",@"10",@"20",@"30",@"40",
                               @"50"]];
    }
    return _pickerViewData;
}


#pragma -mark- UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return [self.pickerViewData count];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *array = [self.pickerViewData objectAtIndex:component];
    return [array count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSArray *array = [self.pickerViewData objectAtIndex:component];
    NSString *title = [array objectAtIndex:row];
    if (component == 0)
    {
        title = [title stringByAppendingString:@"时"];
    }
    else if (component == 1)
    {
        title = [title stringByAppendingString:@"分"];
    }
    return title;
}


#pragma -mark- UIPickerViewDelegate


#pragma -mark- UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"button index %ld", (long)buttonIndex);
    if ([alertView isEqual:self.timePickerAlertView])
    {
        if (1 == buttonIndex)
        {
            switch (self.timePickerAlertView.tag)
            {
                case 1:
                    _controlPeriods.period1.startHour = [self.pickerView selectedRowInComponent:0];
                    _controlPeriods.period1.startMinute = [self.pickerView selectedRowInComponent:1];
                    [self.period1OpenTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period1.startHour, _controlPeriods.period1.startMinute] forState:UIControlStateNormal];
                    break;
                case 2:
                    _controlPeriods.period1.endHour = [self.pickerView selectedRowInComponent:0];
                    _controlPeriods.period1.endMinutes = [self.pickerView selectedRowInComponent:1];
                    [self.period1CloseTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period1.endHour, _controlPeriods.period1.endMinutes] forState:UIControlStateNormal];
                    break;
                case 3:
                    _controlPeriods.period2.startHour = [self.pickerView selectedRowInComponent:0];
                    _controlPeriods.period2.startMinute = [self.pickerView selectedRowInComponent:1];
                    [self.period2OpenTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period2.startHour, _controlPeriods.period2.startMinute] forState:UIControlStateNormal];
                    break;
                case 4:
                    _controlPeriods.period2.endHour = [self.pickerView selectedRowInComponent:0];
                    _controlPeriods.period2.endMinutes = [self.pickerView selectedRowInComponent:1];
                    [self.period2CloseTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period2.endHour, _controlPeriods.period2.endMinutes] forState:UIControlStateNormal];
                    break;
                case 5:
                    _controlPeriods.period3.startHour = [self.pickerView selectedRowInComponent:0];
                    _controlPeriods.period3.startMinute = [self.pickerView selectedRowInComponent:1];
                    [self.period3OpenTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period3.startHour, _controlPeriods.period3.startMinute] forState:UIControlStateNormal];
                    break;
                case 6:
                    _controlPeriods.period3.endHour = [self.pickerView selectedRowInComponent:0];
                    _controlPeriods.period3.endMinutes = [self.pickerView selectedRowInComponent:1];
                    [self.period3CloseTime setTitle:[NSString stringWithFormat:@"%02ld: %ld0", (unsigned long)_controlPeriods.period3.endHour, _controlPeriods.period3.endMinutes] forState:UIControlStateNormal];
                    break;
                    
                default:
                    break;
            }
        }
    }
    else
    {
        if (0 == buttonIndex)
        {
            [self.navigationController popViewControllerAnimated:YES];
           
        }
        if (1 == buttonIndex)//confirm button
        {
            [[APIs sharedInstance] setControlPeriod:_controlPeriods
                                           password:_deviceInfo.password
                                            success:^{
                                                [[[UIAlertView alloc] initWithTitle:@"✅设置成功"
                                                                            message:nil
                                                                           delegate:self
                                                                  cancelButtonTitle:@"好的"
                                                                  otherButtonTitles:nil, nil] show];
                                            }
                                            failure:^(NSInteger errorCode) {
                                                [[[UIAlertView alloc] initWithTitle:@"❌设置失败"
                                                                            message:@"网络异常，请稍候重试"
                                                                           delegate:nil
                                                                  cancelButtonTitle:@"好的"
                                                                  otherButtonTitles:nil, nil] show];
                                            }];
        }
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
